package cn.edu.hubu.hubusmartzoohouse.service.impl;

import cn.edu.hubu.hubusmartzoohouse.entity.Animal;
import cn.edu.hubu.hubusmartzoohouse.mapper.AnimalMapper;
import cn.edu.hubu.hubusmartzoohouse.service.AnimalService;
import cn.edu.hubu.hubusmartzoohouse.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnimalImpl implements AnimalService {

    @Autowired
    private AnimalMapper animalMapper;

    @Override
    public ResultVo addAnimal(Animal animal) {
        int i = animalMapper.insert(animal);
        if (i>0){
            return new ResultVo(200, "添加动物成功", null);
        } else {
            return new ResultVo(500, "操作有误", null);
        }
    }


    @Override
    public ResultVo listAnimal() {
        List<Animal> list = animalMapper.listAll();
        ResultVo vo = new ResultVo(200, "查询成功", list);
        return vo;
    }

    @Override
    public ResultVo logicDeleteAnimal(int animalId) {
        int i = animalMapper.updateIsDelete(animalId, 1);
        return i>0? new ResultVo(200, "删除成功！", null) :
                new ResultVo(500, "删除失败!", null);
    }

    @Override
    public ResultVo get(int animalId) {
        Animal animal = animalMapper.selectById(animalId);
        return new ResultVo(200, "查询成功！", animal);
    }

    @Override
    public ResultVo modifyAnimal(Animal animal) {
        int i = animalMapper.updateAnimal(animal);
        if (i>0){
            return new ResultVo(200, "修改成功！", animal);
        } else {
            return new ResultVo(500, "修改失败！", animal);

        }
    }

    @Override
    public ResultVo searchAnimalById(int animalId) {
        Animal animal = animalMapper.selectById(animalId);
        if (animal != null){
            return new ResultVo(200, "查询成功！", animal);
        } else {
            return new ResultVo(500, "查询失败: 未找到动物信息", null);
        }
    }

    @Override
    public ResultVo searchAnimalByName(String animalName) {
        Animal animal = animalMapper.selectByName(animalName);
        if (animal!=null){
            return new ResultVo(200, "查询成功！", animal);
        } else {
            return new ResultVo(500, "查询失败: 未找到动物信息", null);
        }
    }


}
